﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ThoiTrang.Business;
using ThoiTrang.Entities;
using ThoiTrang.Data;
using System.Text;

public partial class adm_brand : System.Web.UI.Page
{
    #region "Page functions"

    public string ckid = "";

    private void _BindGreeting()
    {
        a_User au=a_UserService.Geta_User(Request.Params["ckid"]);
        if (au != null)
        {
            this.lblGretting.Text = "Hi " + au.Firstname + " " + au.Lastname;
        }
    }

    private void _ReadParam()
    {
            try
            {
                if (Request.Params["catid"] != null && Request.Params["act"]==null)
                {
                    a_Brand acy = a_BrandService.Geta_Brand(int.Parse(Request.Params["catid"]));
                    if (acy != null)
                    {
                        this.txtBrandEN.Text = acy.BrandNameEN;
                        this.txtBrandVN.Text = acy.BrandNameVN;
                        this.btnUpdate.Visible = true;
                        this._BindRepeater();
                    }
                }
                else if (Request.Params["catid"] != null && Request.Params["act"] == "del")
                {
                    //Kiem tra xem co category nao khong?
                    a_BrandService.Deletea_Brand(int.Parse(Request.Params["catid"]));
                    CommonFunc.ShowAlertMessage(this.lblAlert,"Delete sucessfully");
                    this._BindRepeater();
                }
                else if (Request.Params["fn"] != null)
                {
                    this._BindRepeater();
                }
            }
            catch (Exception ex)
            {
                //throw new exception here
            }
    }

    public string GetActiveClass(string fn)
    {
        if (Request.Params["fn"] != null && Request.Params["fn"] == fn)
        {
            return "active";
        }
        else
        {
            return "";
        }
    }

    public string GetActiveClassAll()
    {
        if (Request.Params["fn"] == null)
        {
            return "active";
        }
        else
        {
            return "";
        }
    }

    private void _BindRepeater()
    {
        if (Request.Params["fn"] == null | (Request.Params["fn"]=="#" | Request.Params["fn"]==""))
        {
            this.rptBrand.DataSource = a_BrandService.Geta_BrandList(a_BrandColumns.BrandNameVN, OrderDirection.ASC);
            this.rptBrand.DataBind();
        }
        else
        {
            //Thuc hien search
            string fn = Request.Params["fn"].ToString();
            this.rptBrand.DataSource = a_BrandService.Geta_BrandList_alphabet(fn, a_BrandColumns.BrandNameVN, OrderDirection.ASC);
            this.rptBrand.DataBind();
        }
    }

    private void _BindAlphabetSearcher()
    {
        DataTable dt = General.ExecSQLStatement("SELECT DISTINCT SUBSTRING(BrandNameVN, 1, 1) AS Name FROM a_Brands ORDER BY Name");
        this.rptAlphabetSearch.DataSource = dt;
        this.rptAlphabetSearch.DataBind();
    }

    public string _CreateLinkEditAndDelete(Int32 catid)
    {
        string hLink = "";
        if (Request.Params["ckid"] != null)
        {
            hLink= "<a href='adm_brand.aspx?ckid=" + Request.Params["ckid"].ToString() + "&catid=" + catid.ToString() + "&fn={0}'><img src='../images/ico_edit.gif' alt='edit' width='12' height='12' /></a><a onclick="+"\""+"return confirm('Are you sure want to delete?')"+"\""+"' href='adm_brand.aspx?ckid=" + Request.Params["ckid"].ToString() + "&catid=" + catid.ToString() + "&act=del" + "&fn={0}'><img src='../images/ico_delete.gif' alt='del' width='16' height='13' /></a>";
        }
        else
        {
            hLink= "<a href='adm_brand.aspx?catid=" + catid.ToString() + "&fn={0}'><img src='../images/ico_edit.gif' alt='edit' width='12' height='12' /></a><a href='adm_brand.aspx?catid=" + catid.ToString() + "&act=del" + "&fn={0}'><img src='../images/ico_delete.gif' alt='del' width='16' height='13' /></a>";
        }
        if(Request.Params["fn"]!=null && (Request.Params["fn"]!="#" | Request.Params["fn"]!=""))
        {
            hLink=hLink.Replace("{0}",Request.Params["fn"].ToString());
        }
        else
        {
            hLink = hLink.Replace("{0}", "");
        }
        return hLink;
    }

    public string _CreateLinkforAll()
    {
        if (Request.Params["ckid"] != null)
        {
            return "adm_brand.aspx?ckid=" + Request.Params["ckid"].ToString();
        }
        else
        {
            return "";
        }
    }

    #endregion

    #region "Event"
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["ckid"] != null)
        {
            ckid = Request.Params["ckid"];
        }
        if (!this.IsPostBack)
        {
            this._BindRepeater();
            this._ReadParam();
            this._BindGreeting();
            this._BindAlphabetSearcher();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            a_Brand acy = new a_Brand();
            acy.BrandNameEN = this.txtBrandEN.Text;
            acy.BrandNameVN = this.txtBrandVN.Text;
            a_BrandService.Createa_Brand(acy);
            CommonFunc.ShowAlertMessage(this.lblAlert, "Add new brand sucessfully");
            this._BindRepeater();
        }
        catch (Exception ex)
        {
            //throw new error
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            a_Brand acy = a_BrandService.Geta_Brand(int.Parse(Request.Params["catid"]));
            if (acy != null)
            {
                acy.BrandNameEN = this.txtBrandEN.Text;
                acy.BrandNameVN = this.txtBrandVN.Text;
                this.btnUpdate.Visible = false;
                a_BrandService.Updatea_Brand(acy);
                CommonFunc.ShowAlertMessage(this.lblAlert, "Update brand sucessfully");
                this._BindRepeater();
            }
        }
        catch (Exception ex)
        {
            //throw new exception here
        }
    }
    #endregion 
}
